package com.geuso.xingdutch.colours;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.geuso.xingdutch.R;
import com.geuso.xingdutch.util.SoundUtil;

public class ColoursListAdapter extends ArrayAdapter<Colour> {
	
	private static final int resource = R.layout.colours_list_item;
	
	public ColoursListAdapter(Context context, ArrayList<Colour> colours){
		super(context, resource, colours);
	}
	
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		// assign the view we are converting to a local variable
		View v = convertView;

		// first check to see if the view is null. if so, we have to inflate it.
		// to inflate it basically means to render, or show, the view.
		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(resource, null);
		}

		Colour c = getItem(position);
		if (c != null){
			TextView dutch = (TextView) v.findViewById(R.id.col_dutch);
			TextView dutch_pronounciation = (TextView) v.findViewById(R.id.col_dutch_pronounce);
			TextView english = (TextView) v.findViewById(R.id.col_english);
			TextView chinese = (TextView) v.findViewById(R.id.col_chinese);
			ImageView play_sound = (ImageView) v.findViewById(R.id.col_play_sound);
			View colour_box = v.findViewById(R.id.col_box);
			
			if (dutch != null)
				dutch.setText(c.getDutch());
			
			if (dutch_pronounciation != null) 
				dutch_pronounciation.setText(c.getDutch_pronounciation());
			
			if (english != null)
				english.setText(c.getEnglish());

			if (chinese != null)
				chinese.setText(c.getChinese());
			
			if (play_sound != null){
				if (c.getSound_file() > 0){
					play_sound.setVisibility(View.VISIBLE);
					SoundUtil.setOnClickPlaySound(play_sound, c.getSound_file());
				}
				else play_sound.setVisibility(View.INVISIBLE);
			}
			
			if (colour_box != null){

				GradientDrawable gd = new GradientDrawable();
		        gd.setColor(c.getColour());
		        gd.setStroke(2, c.getBorderColour());
		        colour_box.setBackground(gd);
			}
			
		}

		return v;
	}
	
	
}
